House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Solution:

  1. public class Solution {
  2. public int rob(int[] a) {
  3. if (a == null || a.length == 0)
  4. return 0;
  5. int n = a.length;
  6. // dp(i) represents the max money by robbing till i-th house
  7. int[] dp = new int[n + 1];
  8. dp[1] = a[0];
  9. for (int i = 2; i <= n; i++) {
  10. dp[i] = Math.max(dp[i - 1], a[i - 1] + dp[i - 2]);
  11. }
  12. return dp[n];
  13. }
  14. }